
import util from "../../common/js/util.js";
var app = getApp();
const updateManager = dd.getUpdateManager();

Page({
  data: {
    SysUserCnName: "请先登录",
    is_login: false,
    version: 0,
    devVersion: "",
    listData: {
      onItemTap: 'handleListItemTap',
      // header: 'list1',
      data: [
        {
          title: '查看时长统计',
          extra: 'icon-youjiantou2',
          func:"goToTimeCount"
        },
        {
          title: '查看打卡记录',
          extra: 'icon-youjiantou2',
          func:"goToCardList"
        },
        {
          title: '查看学习笔记',
          extra: 'icon-youjiantou2',
          func:"goToNoteList"
        },
        {
          title: '查看积分记录',
          extra: 'icon-youjiantou2',
          func:"goToIntegralList"
        },
        {
          title: '查看奖惩记录',
          extra: 'icon-youjiantou2',
          func:"goToRewards"

        },
        {
          title: '清理登录数据',
          extra: 'icon-youjiantou2',
          func:"clearStorage"

        },
        {
          // 版本号:1.0.8.16
          // test 小程序 版本号:1.0.0.63
          type:"version",
          title: '1.0.0.66',
          arrow: 'horizontal',
        }
      ]
    },
    userInfo:{}
  },
  onLoad() {
    // debugger
    
  },
  onShow() {
    this.loadFn();
  },
  loadFn(){
    if (util.isEmpty(app.prototype.$userName)) {
      util.getUserMessage();
      if(util.isNotEmpty(app.prototype.$userName)){
        this.setData({
          is_login: true,
          SysUserCnName: app.prototype.$userName,
          SysUserName: app.prototype.$userName
        })
      }
      
    } else {
      this.setData({
        is_login: true,
        SysUserCnName: app.prototype.$userName,
        SysUserName: app.prototype.$userName
      })
    }
    this.getIsLogin();
  },
  handleListItemTap(e) {
    // console.log(e.currentTarget.dataset);
    let index= e.currentTarget.dataset.index
    let item=this.data.listData.data[index]
    // dd.showToast({
    //   content: `第${e.currentTarget.dataset.index}个Item`,
    //   success: (res) => {
    //   },
    // });
    if(item.func){
      this[`${item.func}`]()
    }

    
  },
  goToLogin() {
    // console.log("sss");
    dd.navigateTo({
      url: `../login/login`,
    });
  },
  goToTimeCount(){
    dd.navigateTo({
      url: `/pages/userTimeCount/userTimeCount`,
    });
  },
  goToCardList() {
    // console.log("sss");
    dd.navigateTo({
      url: `/pages/userCardList/userCardList`,
    });
  },
  goToNoteList() {
    // console.log("sss");
    dd.navigateTo({
      url: `/pages/note/noteList/noteList`,
    });
  },
  goToIntegralList(){
    dd.navigateTo({
      url: `/pages/integralList/integralList`,
    });
  },
  goToRewards(){
    dd.navigateTo({
      url: `/pages/userRewardsList/userRewardsList`,
    });
  },
  dingdingBinding() {
    
   
  },
  getCardTypeValue(cardType){
    let value="";
    switch (cardType) {
      case 0:
        value="未满勤";
        break;
      case 1:
        value="满勤";
        break;
      case 2:
        value="全勤";

        break;          
      default:
        break;
    }
    return value;
  },
  getIsLogin(){
    let api = "/user/info/getIsLogin", list = {  };
    app.$post(api, list).then(re => {
      try {
        if (re.code == 401) {
          app.authLoginFn().then(res=>{
           // console.log("--detail--res:",res);
            this.getUserInfo();

          })
        }
        else {
          this.getUserInfo();
        };
      } catch (error) {
      }
    }).catch(err => {
     
    });
  },
  getUserInfo(){
    let api = "/user/info/getUserInfo", list = {};
    app.$post(api, list).then(re => {
      dd.stopPullDownRefresh();
      try {
         if (re.code == 200) {


          re.data.monthCardTypeValue= this.getCardTypeValue(re.data.monthCardType);
          re.data.yearCardTypeValue= this.getCardTypeValue(re.data.yearCardType);

          this.setData({
            userInfo:re.data
          })
          
        }else {
          util.message(re.message)
        };
      } catch (error) {
        
      }

    }).catch(err => {
      
      
    });
  },
  getStorageSize() {
    let that = this;
    dd.getStorageInfo({
      success: res => {
        //console.log(res.keys);
        //console.log(res.limitSize);
        let size = res.currentSize;
        if (size < 1024) {
          that.data.storageSize = size + 'B';
        } else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
          that.storageSize = Math.floor(size / 1024 * 100) / 100 + 'KB';
        } else if (size / 1024 / 1024 >= 1) {
          that.storageSize = Math.floor(size / 1024 / 1024 * 100) / 100 + 'M';
        }
      }
    });
  },
  clearStorageFn() {
    let that = this;
    updateManager.onCheckForUpdate(function (res) {

      if (res.hasUpdate) {
        updateManager.onUpdateReady(function (ret) {
          dd.confirm({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function (res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate();
                that.clearFn();
              }
            }
          })
        })
      } else {
        that.clearStorage();
      }
    })
  },
  onPullDownRefresh() {
    // 页面被下拉
    this.getIsLogin()
  },
  clearFn() {
    dd.clearStorageSync();
    //重新获取并显示清除后的缓存大小
    //dd.getStorageSize();
    //dd.showToast({title:'清除成功'});
    util.message('清除成功');
    dd.navigateTo({
      url: '../login/login'
    })
  },
  clearStorage() {
    //let storage=dd.getStorageSync({key:"historyRoute"}).data;
    // let path = storage["/pages/entenring_Informs/entenring_Informs"].url || storage["/pages/entering_Diary_Item/entering_Diary_Item"].url;
    let that = this;
    dd.confirm({
      title: '提示',
      content: '确定登录数据吗?将会使您退出登录!',
      confirmButtonText: '立即清除',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm) {
          try {
            that.clearFn();
            that.setData({
              is_login:false
            })
            app.prototype.$Token="";
          } catch (error) {
            console.log(error);
          }
        }
      },
    });
  }
});
